package com.view.jurisdictionmanage;

import com.bean.Power;
import com.bean.Role;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTxt;
import com.service.PowerService;
import com.service.RoleService;
import com.service.UserService;
import com.service.impl.PowerServiceImpl;
import com.service.impl.RoleServiceImpl;
import com.service.impl.UserServiceImpl;
import com.util.MyCheckBox;
import com.view.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RoleUpdateDialog extends JDialog {
    private LovoButton addBtn;
    private LovoButton cancelBtn;

    private MyCheckBox roleCbx;
    private JLabel nameLeb;

    private int key;
    private MainFrame frame;
    private UserService userService = new UserServiceImpl();
    private RoleService roleService = new RoleServiceImpl();
    private PowerService powerService = new PowerServiceImpl();

    public RoleUpdateDialog(MainFrame frame,int key) {
        // TODO Auto-generated constructor stub
        super(frame,"修改用户",true);
        this.frame = frame;
        this.key = key ;
        this.setBounds(350, 250, 520, 400);
        this.setLayout(null);
        cbxFlush();
        this.init();
        this.setVisible(true);
    }

    public void cbxFlush(){
        List<String> powerList = new ArrayList<>();
        List<Role> list = new ArrayList<>();
        list = roleService.getAllRole();
        for (Role role:list) {
            int id = role.getId();
            if(id == key){
                List<Power> powers = new ArrayList<>();
                powers = role.getPowerList();
                for (Power power:powers) {
                    String powerName = power.getPowerName();
                    powerList.add(powerName);
                }
            }
        }
        System.out.println(powerList);
        String[] strings = powerList.toArray(new String[powerList.size()]);
        String[] strs = powerService.getAllPowers();
        roleCbx = new MyCheckBox("权限",strs,20,100,this);
        roleCbx.setItem(strings);
    }

    private void init() {
        // TODO Auto-generated method stub

        nameLeb = new JLabel();
        nameLeb.setBounds(20,10,350,50);
        String name = roleService.selectRoleName(key);
        nameLeb.setText("角色:"+name+"");
        this.add(nameLeb);


        this.addBtn = new LovoButton("添加", 120, 320, this);
        this.cancelBtn = new LovoButton("取消", 270, 320, this);

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String[] strings = roleCbx.getItem();
                java.util.List<String> rolenames = Arrays.asList(strings);

                if(roleService.updateRolePower(key,rolenames)){
                    JOptionPane.showMessageDialog(null,"修改成功");
                    RoleUpdateDialog.this.dispose();
                    frame.flush();
                }else {
                    JOptionPane.showMessageDialog(null,"修改失败");
                }
            }
        });

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RoleUpdateDialog.this.dispose();
            }
        });

    }

}
